//额外一些方法
import { tyqSDK } from "./tyq-sdk";
import tyqSdkConfig from "./tyq-sdk-config";
import { Http } from "./weHttp";

export default class tyqExtend {
    //获取字节跳动的openid
    public static getBytedanceOpenid(_code: string, _anonymous_code: string) {
        let data = {
            appid: tyqSdkConfig.appId,
            secret: tyqSdkConfig.AppSecret,
            code: _code,
            anonymous_code: _anonymous_code,
        }
        console.log("jscode2session");
        Http.get("mgplatform/api/apps/jscode2session", data, (err, res) => {
            if (res.errmsg) {
                console.error(res.errmsg);
            } else {
                tyqSDK.platformLogin(res.openid);
                console.log(res.openid);
            }

        }, "https://minigame.zijieapi.com/")
    }

    public static getDeviceId() {
        let deviceId = "";
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            deviceId = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/DeviceIdUtil", "getDeviceId", "()Ljava/lang/String;");
        } else if (cc.sys.os == cc.sys.OS_IOS) {

        }
        if (!deviceId) {
            cc.error("获取设备标识码错误");
        }
        return deviceId;
    }

    public static downloadApk(url: string) {
        if (cc.sys.os == cc.sys.OS_ANDROID) {
            jsb.reflection.callStaticMethod("org/cocos2dx/javascript/DownloadHelp", "downloadAPK", "(Ljava/lang/String;)V", url);
        }
    }

    public static delayShowBtn(node: cc.Node) {
        node.active = false;
        let delayTime = tyqSDK.getSwitchValue("tyq_btnDelayShow");
        setTimeout(() => {
            if (node && cc.isValid(node)) {
                node.active = true;
            }
        }, parseFloat(delayTime) * 1000);
    }
}